package pl.edu.mimuw.irs.core.model.design.resource;

import java.io.Serializable;


/**
 * @author Jakub Rauch
 * Created on: 2009-05-07
 */
public class RoleInstance implements Serializable, IMember {

	private static final long serialVersionUID = -7131721966011350776L;
	
	// *** Attributes
	
	private Role role;
	private IHolder<RoleInstance> parent;
	private Double modifier;
	private Double calculatedModifier;
	// Getters and setters
	public Role getRole() {
		return role;
	}
	public void setRole(Role role) {
		this.role = role;
	}
	public IHolder<RoleInstance> getHolder() {
		return parent;
	}
	public void setParent(IHolder<RoleInstance> parent) {
		if (this.parent == parent) return;
		IHolder<RoleInstance> oldParent = null;
		if (this.parent != null) oldParent = this.parent;
		this.parent = parent;
		if (oldParent != null) oldParent.remove(this);
	}
	public Double getModifier() {
		if (modifier == null) modifier = 1d;
		return modifier;
	}
	public void setModifier(Double modifier) {
		this.modifier = modifier;
	}
	public Double getCalculatedModifier() {
		return calculatedModifier;
	}
	public void setCalculatedModifier(Double calculatedModifier) {
		this.calculatedModifier = calculatedModifier;
	}
	
	// *** Methods
	
	public RoleInstance(Role role, Double modifier, Double calculatedModifier) {
		setRole(role);
		setModifier(modifier);
		setCalculatedModifier(calculatedModifier);
	}
	
	public RoleInstance(Role role, double modifier) {
		setRole(role);
		setModifier(modifier);
	}
	
	public RoleInstance(Role role) {
		this(role, 1);
	}
	/*
	@Override
	public boolean equals(Object obj) {
		if (obj instanceof RoleInstance) {
			RoleInstance roleInstance = (RoleInstance) obj;
			if (roleInstance.getRole().equals(getRole()) &&
					roleInstance.getTimeCost().equals(getTimeCost())) {
				return true;
			}
		}
		return false;
	}
	
	@Override
	public int hashCode() {
		return getRole().hashCode() + new Integer(getTimeCost()).hashCode();
	}
	*/
	@Override
	public String toString() {
		return getRole().getName();
	}
	
	public RoleInstance copy() {
		RoleInstance roleInstance = new RoleInstance(getRole(), getModifier());
		return roleInstance;
	}

	@Override
	public void delete() {
		getHolder().remove(this);
	}
}
